package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.WebConfig;
import com.chen.dto.R;
import com.chen.service.WebConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: chen
 * @Date: 2021/4/22 3:03 PM
 * @Version 1.0
 */
@RestController
@Api(tags = "网站配置控制器")
@RequestMapping("/webConfigs")
public class WebConfigController {

    @Autowired
    private WebConfigService webConfigService;

    @GetMapping
    @ApiOperation(value = "分页查询网站配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "资源名称"),
            @ApiImplicitParam(name = "type", value = "资源类型"),
            @ApiImplicitParam(name = "current", value = "当前页数"),
            @ApiImplicitParam(name = "size", value = "每页数量")
    })
    @PreAuthorize("hasAuthority('web_config_query')")
    public R<Page<WebConfig>> list(Page<WebConfig> page,
                                   String name,
                                   String type) {
        QueryWrapper<WebConfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(type)) {
            queryWrapper.like("type", type);
        }
        queryWrapper.orderByDesc("sort");
        Page<WebConfig> page1 = webConfigService.page(page, queryWrapper);
        return R.ok(page1);

    }

    @PostMapping
    @ApiOperation(value = "新增一个WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "webConfig 的json数据")
    })
    @PreAuthorize("hasAuthority('web_config_create')")
    public R add(@RequestBody @Validated WebConfig webConfig) {
        boolean save = webConfigService.save(webConfig);
        if (save) {
            return R.ok();
        }
        return R.fail("新增失败");
    }


    @PatchMapping
    @ApiOperation(value = "修改一个WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "webConfig 的json数据")
    })
    @PreAuthorize("hasAuthority('web_config_update')")
    public R update(@RequestBody @Validated WebConfig webConfig) {
        boolean save = webConfigService.updateById(webConfig);
        if (save) {
            return R.ok();
        }
        return R.fail("修改失败");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除的数据的ids")
    })
    @PreAuthorize("hasAuthority('web_config_delete')")
    public R update(@RequestBody String[] ids) {
        if (ids == null || ids.length == 0) {
            return R.fail("删除时需要给删除数据的Id");
        }
        boolean b = webConfigService.removeByIds(Arrays.asList(ids));
        if (b) {
            return R.ok();
        }
        return R.fail("删除失败");
    }

    @GetMapping("/banners")
    public R<List<WebConfig>> banners(){

        List<WebConfig> web_banner = webConfigService.list(new LambdaQueryWrapper<WebConfig>()
                .eq(WebConfig::getStatus, 1)
                .eq(WebConfig::getType, "WEB_BANNER")
                .orderByDesc(WebConfig::getSort));

        return R.ok(web_banner);
    }



}
